package com.nbjtjc.safe.service.edu;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.*;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.edu.EduDailyInfo;
import com.nbjtjc.safe.model.edu.EduDailyParticipant;
import com.nbjtjc.safe.model.edu.EduDailyRecord;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EduDailyInfoService {
	@Autowired
	private EduDailyInfoMapper eduDailyInfoMapper;
	@Autowired
	private EduDailyRecordMapper eduDailyRecordMapper;
	@Autowired
	private EduDailyParticipantMapper eduDailyParticipantMapper;
	@Autowired
	private EduDailyNotifyMapper  eduDailyNotifyMapper;
	@Autowired
	private EduDailyUserEvaluateMapper eduDailyUserEvaluateMapper;
	@Autowired
	private EduDailyExamineListMapper eduDailyExamineListMapper;
	@Autowired
	private EduDailyExamineScoreMapper eduDailyExamineScoreMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    
	  public Result add(EduDailyInfo eduDailyInfo){
		        Result result=new Result();
		        if(ObjectUtil.isNull(eduDailyInfo)
//		        		||ObjectUtil.isNull(eduDailyInfo.getReqInvestId())
		        		){		            
		        	result.setFalse("日常培训信息不能为空");
		            return result;
		        }
//主键为空或库中无此主键则为新建，插入数据
		        if(ObjectUtil.isNull(eduDailyInfo.getId())|| ObjectUtil.isNull(eduDailyInfoMapper.selectByPrimaryKey(eduDailyInfo.getId()))){
		        	eduDailyInfo.setCancel(0);
		        	int  companeyId= MyShiroUtils.getCompanyId();
		        	eduDailyInfo.setCompanyId(companeyId);
		        	eduDailyInfo.setCompanyName(sysDeptMapper.findNameById(companeyId));
		        	eduDailyInfo.setCreateDate(new Date());
		        	eduDailyInfo.setCreateId(MyShiroUtils.getSessionUserId());
		        	
		        	int res=eduDailyInfoMapper.insertSelective(eduDailyInfo);
			        if(res==0){
			            result.setFalse("插入日常培训信息失败");
			            return result;
			        }
		        	 int t=eduDailyInfo.getId();

			    	if(ObjectUtil.isNull(t)){
			            result.setFalse("日常培训信息ID为空");
			        	return result;
				    }
				 //新建参与人员列表
			    	result = insertEduDailyParticipant(eduDailyInfo);
			    	if(result.isSuccess()==false){
			            return result;
			        }
				 //新建一个record
			        res = eduDailyRecordMapper.createByInfoId(t);
			        
			    	if(res==0){
			            result.setFalse("新建日常培训记录失败");
			            return result;
			        }
			    	//新建通知信息
			    	if(ObjectUtil.isNotNull(eduDailyInfo.getEduDailyNotify())&&eduDailyInfo.getEduDailyNotify().size()==1){
			    		if(ObjectUtil.isNotNull(eduDailyInfo.getEduDailyNotify().get(0).getId())){
				            result.setFalse("新建通知信息的ID应当为空，但此处有值");
				            return result;
			    		}
			    		eduDailyInfo.getEduDailyNotify().get(0).setInfoId(t);
			    		eduDailyInfo.getEduDailyNotify().get(0).setCancel(0);
			    		res = eduDailyNotifyMapper.insertSelective(eduDailyInfo.getEduDailyNotify().get(0));
				        
				    	if(res==0){
				            result.setFalse("新建通知信息失败");
				            return result;
				        }
				    }
		        }else
		        {
		        	eduDailyInfo.setModifyDate(new Date());
		        	eduDailyInfo.setModifyId(MyShiroUtils.getSessionUserId());
		        	//更新日常培训信息主表
		        	int res=eduDailyInfoMapper.updateByPrimaryKeySelective(eduDailyInfo);
		        	 if(res==0){
		        		 result.setFalse("更新日常培训信息失败");
				         return result;

		        	 }
		        	 int t=eduDailyInfo.getId();
		        	 //更新参与人员列表
		        	 if(CollectionUtil.isNotEmpty(eduDailyInfo.getEduDailyParticipants())){
		        		 //删除旧的
		        		 res = eduDailyParticipantMapper.deleteByInfoId(eduDailyInfo.getId());
//		        		 res = eduDailyParticipantMapper.deleteByInfoIdAndCompId(eduDailyInfo.getId(),MyShiroUtils.getCompanyId());//0912,只更新本公司的人员

		        		 
			        	 if(res==0){
			        		 result.setFalse("删除旧参与人员列表失败");
					         return result;
			        	 }
		        		 //插入新的
				      	  result = insertEduDailyParticipant(eduDailyInfo);
				    	  if(result.isSuccess()==false){
				             return result;
				         }
		        	 }
				    	//新建通知信息
				    	if(ObjectUtil.isNotNull(eduDailyInfo.getEduDailyNotify())&&eduDailyInfo.getEduDailyNotify().size()==1){
				    		eduDailyInfo.getEduDailyNotify().get(0).setInfoId(t);
				    		eduDailyInfo.getEduDailyNotify().get(0).setCancel(0);
				    		if(ObjectUtil.isNotNull(eduDailyInfo.getEduDailyNotify().get(0).getId())){
					        	eduDailyNotifyMapper.deleteByPrimaryKey(eduDailyInfo.getEduDailyNotify().get(0).getId());
					        }
				    		res = eduDailyNotifyMapper.insertSelective(eduDailyInfo.getEduDailyNotify().get(0));
					        
					    	if(res==0){
					            result.setFalse("新建通知信息记录失败");
					            return result;
					        }
					    }
		        }
		        result.setData(eduDailyInfo);

		        return result;
		    }
	  
	   	 //更新参与人员列表
	  private Result updateEduDailyParticipant(EduDailyInfo eduDailyInfo){
        Result result=new Result();

	   	 if(CollectionUtil.isNotEmpty(eduDailyInfo.getEduDailyParticipants())){
	   		 //删除旧的
	   		 int res = eduDailyParticipantMapper.deleteByInfoId(eduDailyInfo.getId());
	       	 if(res==0){
	       		 result.setFalse("删除旧参与人员列表失败");
			     return result;
	       	 }
	   		 //插入新的
	      	  result = insertEduDailyParticipant(eduDailyInfo);
	    	  if(result.isSuccess()==false){
	             return result;
	         }
	   	 }
    	result.setData(eduDailyInfo.getEduDailyParticipants());
    	return result;
	  }
	  
	    private Result insertEduDailyParticipant(EduDailyInfo eduDailyInfo){
	        Result result=new Result();
	    	if(CollectionUtil.isEmpty(eduDailyInfo.getEduDailyParticipants())){
	            result.setFalse("参与人员插入对象为空");
	        	return result;

		    }
	    	//将getEduDailyParticipants的edu_daily_info_id都设为当前eduDailyInfo的ID
	    	List<EduDailyParticipant> eduDailyParticipants =  eduDailyInfo.getEduDailyParticipants();
			for(EduDailyParticipant eduDailyParticipant: eduDailyParticipants){
				eduDailyParticipant.setEduDailyInfoId(eduDailyInfo.getId());
				eduDailyParticipant.setCancel(0);
			}
			
	    	int res = eduDailyParticipantMapper.insertList(eduDailyParticipants);
	    	if(res==0){
	            result.setFalse("新建参与人员列表失败");
	            return result;
	        }
	    	result.setData(eduDailyParticipants);
	    	return result;
	    }

	  
	  public Result delete(int eduDailyInfoId){
	      Result result=new Result();
	      if(ObjectUtil.isNull(eduDailyInfoId)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      int res = eduDailyInfoMapper.deleteByPrimaryKey(eduDailyInfoId);
	      if(res==0){
	    	  result.setFalse("删除失败");
		      return result;
	
	      }
	      return result;
	  }
	  
	  public Result delete(EduDailyInfo eduDailyInfo){
	      Result result=new Result();
	      if(ObjectUtil.isNull(eduDailyInfo.getId())){
	          result.setFalse("作废对象id不能为空");
	          return result;
	      }
	      int infoId = eduDailyInfo.getId();
	      //将info主表标记为删除
	      int res = eduDailyInfoMapper.cancelById(infoId);
	      if(res==0){
	    	  result.setFalse("作废info失败");
		      return result;
	
	      }
	      //将评价表标记为删除
	      res= eduDailyUserEvaluateMapper.cancelByInfoId(infoId);
	      if(res==0){
	    	  result.setFalse("作废用户评价失败");
		      return result;
	
	      }
	      //删除参与人员表
	      res = eduDailyParticipantMapper.deleteByInfoId(infoId);

//	      res = eduDailyParticipantMapper.cancelByInfoId(infoId);
	      if(res==0){
	    	  result.setFalse("删除参与人员失败");
		      return result;
	
	      }
	      //将record表标记为删除
	      res = eduDailyRecordMapper.cancelByInfoId(infoId);
	      if(res==0){
	    	  result.setFalse("作废record失败");
		      return result;
	
	      }
	      	//删除考核表
	      EduDailyRecord  eduDailyRecord = eduDailyRecordMapper.findByInfoId(infoId);
	     int recId =  eduDailyRecord.getId();
	      res = eduDailyExamineListMapper.deleteByRecId(recId);

//	      res = eduDailyExamineListMapper.cancelByRecId(recId);
		      if(res==0){
		    	  result.setFalse("删除考核表失败");
			      return result;
		
		      }
//	      		//作废考核分数表
//		      List<EduDailyExamineList> eduDailyExamineLists = eduDailyExamineListMapper.findByRecordId(recId);
//			  for(EduDailyExamineList eduDailyExamineList:eduDailyExamineLists ){
//				  res = eduDailyExamineScoreMapper.cancelByExamId(eduDailyExamineList.getId());
//			      if(res==0){
//			    	  result.setFalse("作废考核分数失败");
//				      return result;
//			
//			      }
//			  }

	      //将notify表标记为删除
	      res = eduDailyNotifyMapper.cancelByInfoId(infoId);
	      if(res==0){
	    	  result.setFalse("作废通知失败");
		      return result;
	
	      }
	      
	      return result;
	  }
	  

//	  public Result delete2(int eduDailyExamineListId){
//	      Result result=new Result();
//	      if(ObjectUtil.isNull(eduDailyExamineListId)){
//	          result.setFalse("删除对象不能为空");
//	          return result;
//	      }
//	      int res = eduDailyInfoMapper.cancelByPrimaryKey(eduDailyExamineListId);
//	      if(res==0){
//	    	  result.setFalse("删除失败");
//		      return result;
//	
//	      }
//	      return result;
//	  }
	  
	  public Result find(EduDailyInfo eduDailyInfo) {
	      Result result = new Result();
	      PageHelper.startPage(eduDailyInfo.getPageCurrent(), eduDailyInfo.getPageSize());
	      List<EduDailyInfo> eduDailyInfos =new ArrayList();

	      if(ObjectUtil.isNull(eduDailyInfo.getUserId())){
		      eduDailyInfos = eduDailyInfoMapper.searchDetail(eduDailyInfo);
	      }else{
	    	  eduDailyInfos=eduDailyInfoMapper.findDetailByUser(eduDailyInfo);
	      }
	      if(CollectionUtil.isNotEmpty(eduDailyInfos)){
	 	     List<Integer> nullArray = new ArrayList();
		      for(EduDailyInfo dailyInfo:eduDailyInfos){
		    	  if(CollectionUtil.isEmpty(dailyInfo.getEduDailyEvaluateUserIds())){
		    		  dailyInfo.setEduDailyEvaluateUserIds(nullArray);
		    	  }
		      }
	      }

	      if(eduDailyInfos.size()==1){
	    	  eduDailyInfoMapper.updateCount(eduDailyInfos.get(0).getId());
	      }
	      	      result.setData(new PageInfo<>(eduDailyInfos));

	      return result;
	  }
	  
//	  public Result find2(EduDailyInfo eduDailyInfo) {
//	      Result result = new Result();
//	      PageHelper.startPage(eduDailyInfo.getPageCurrent(), eduDailyInfo.getPageSize());
//	      List<EduDailyInfo> eduDailyInfos = eduDailyInfoMapper.findByUser(eduDailyInfo);
//	      if(eduDailyInfos.size()==1){
//	    	  eduDailyInfoMapper.updateCount(eduDailyInfos.get(0).getId());
//	      }
//	      result.setData(new PageInfo<>(eduDailyInfos));
//
//	      return result;
//	  }
	  
	  public Result show(int id) {
	      Result result = new Result();
	      //更新评价数量
	      eduDailyInfoMapper.updateCount(id);
	      EduDailyInfo eduDailyInfo = new EduDailyInfo();
	      eduDailyInfo.setId(id);
	      List<EduDailyInfo> eduDailyInfos = eduDailyInfoMapper.searchDetail(eduDailyInfo);
	      if(CollectionUtil.isNotEmpty(eduDailyInfos)){
		      PageHelper.startPage(1, 5);
		      result.setData(new PageInfo<>(eduDailyInfos));
	      }

	      return result;
	  }
}
